#include "mvc/controller/configuration_controller.h"
#include "mvc/model/project.h"
#include <iostream>
#include "common/settings.h"

#include <QDebug>
Configuration_controller::Configuration_controller()
{

}


bool Configuration_controller::read_file_config(configurations_parameters_uavs *cpu,QString currentProject)
{
//    cout << "Configuration_Controller.read file config " << endl;
    _name_file = QString(currentProject).append(DATS_PREFIX).append("CONFIG.DAT");
    //_name_file = currentProject;

    QFile file(_name_file);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        std::cout << "Cannot find file in Configuration_Controller: read_file_config: " << _name_file.toStdString() <<std::endl;
        return false;
    }
    QTextStream in(&file);

    QString line;
    QStringList elementLine;
    while(!in.atEnd())
    {
        line = in.readLine();
        elementLine = line.split(QRegExp("\\s+"));
        if(line.contains("!options")){
            cpu->offset = elementLine.at(1).toFloat();
            if(elementLine.at(2).contains("1"))
                cpu->mode = true;
            else
                cpu->mode = false;

            if(elementLine.at(3).contains("1"))
                cpu->matriz = true;
            else
                cpu->matriz = false;

            if(elementLine.at(4).contains("1"))
                cpu->normal = true;
            else
                cpu->normal = false;

            if(elementLine.at(5).contains("1"))
                cpu->velocity = true;
            else
                cpu->velocity = false;
            //qDebug() << elementLine;
        }

        if(line.contains("!AoA")){
            cpu->view_angle = elementLine.at(1).toFloat();
        }

        if(line.contains("!Vinf")){
            cpu->vector_direccion_x = elementLine.at(1).toFloat();
            cpu->vector_direccion_y = elementLine.at(2).toFloat();
            cpu->vector_direccion_z = elementLine.at(3).toFloat();
        }

        if(line.contains("!Vc")){
            cpu->velocidad_caracteristica = elementLine.at(1).toFloat();
            cpu->longitud_caracteristica = elementLine.at(2).toFloat();
        }

        if(line.contains("!T")){
            cpu->t_box = elementLine.at(1).toFloat();
            cpu->nmax_box = elementLine.at(2).toFloat();
        }

        if(line.contains("!Chord")){
            cpu->chord_box = elementLine.at(1).toFloat();
            cpu->span_box = elementLine.at(2).toFloat();
        }

        if(line.contains("!Plane")){
            cpu->position_box = elementLine.at(1).toFloat();

        }

        if(line.contains("!Number")){
            cpu->nthreads_box = elementLine.at(1).toFloat();

        }

    }

    file.close();

    return true;
}



void Configuration_controller::make_file_config(configurations_parameters_uavs *cpu, QString currentProject)
{

    _name_file = QString(currentProject).append(DATS_PREFIX).append("CONFIG.DAT");
    QFile conf_file(_name_file);
    QStringList listParameters = get_parameters(cpu);

    if(!conf_file.open( QIODevice::WriteOnly )){
        return;
    }

    QStringList lstLines;
    lstLines << get_begin_file() << get_config_parameters(listParameters) << get_final_file();

    write_content(&conf_file,lstLines);
    conf_file.flush();
    conf_file.close();
}

QStringList Configuration_controller::get_parameters(configurations_parameters_uavs *cpu)
{
    QStringList data;

    QString offset(QString::number(cpu->offset));
    QString mode;
    QString matriz;
    QString normal;
    QString velocity;
    QString sim_all_configs;

    if(cpu->mode == true)
        mode = "1";
    else
        mode = "0";
    if(cpu->matriz == true)
        matriz = "1";
    else
        matriz = "0";
    if(cpu->normal == true)
        normal = "1";
    else
        normal = "0";
    if(cpu->velocity == true)
        velocity = "1";
    else
        velocity = "0";
    data << "    " << offset << "   " << mode << "     " << matriz << "   " << normal << "   " << velocity << "     " <<"!options: NOP1 NOP2 NOP3 NOP4 NOP5\n";

    QString theta(QString::number(cpu->view_angle));
    data << "\t" << theta << "                    " << "!AoA [deg]\n";

    QString vectorX(QString::number(cpu->vector_direccion_x));
    QString vectorY(QString::number(cpu->vector_direccion_y));
    QString vectorZ(QString::number(cpu->vector_direccion_z));
    data << "   " << vectorX << "      " << vectorY << "    " << vectorZ << "  " << "!Vinf [m/s]\n";

    QString vc(QString::number(cpu->velocidad_caracteristica));
    QString lc(QString::number(cpu->longitud_caracteristica));
    data << "  "<< vc << "      " << lc<< "           " << "!Vc   [m/s]   Lc [m]\n";

    QString t(QString::number(cpu->t_box));
    QString nmax(QString::number(cpu->nmax_box));
    data << "   " << t << "       " << nmax<< "             " << "!T          NMAX\n";

    QString chord(QString::number(cpu->chord_box));
    QString span(QString::number(cpu->span_box));
    data << "    " << chord << "     " << span<< "          "<<"!Chord[m] Span[m]\n";

    QString trefftz(QString::number(cpu->position_box));
    data << "    " << trefftz<< "                     " << "!Plane position for velocity field\n";

    QString threads(QString::number(cpu->nthreads_box));
    data << "    " << threads << "                      " << "!Number of threads\n";

    data << "\n";
    return data;
}

int Configuration_controller::write_content(QFile *file, QStringList lstLines)
{
    QTextStream writeData(file);

    foreach(QString writeLine, lstLines)
    {
        writeData << writeLine ;
    }
    return 0;
}

QStringList Configuration_controller::get_config_parameters(QStringList parameters)
{
    QStringList data;

    foreach(QString writeLine, parameters)
    {
        data << writeLine ;
    }
    data << " \n" ;
    return data;

}

QStringList Configuration_controller::get_begin_file()
{
    QStringList data;
    data << "Version 1 of HALE UAV Sensor-Craft configuration\n";
    return data;
}


QStringList Configuration_controller::get_final_file()
{
    QStringList data;

    data << "NOP1 = NC offset for the generation of wake @ trailing edge of aft-wing \n";
    data << "           integer value\n";
    data << "           min. value 0 , max. value < NC aft-wing ib\n";
    data << " \n";
    data << "NOP3 = Solution Mode\n";
    data << "           0 --> Data check only\n";
    data << "           1 --> Execution\n";
    data << " \n";
    data <<"NOP5 = Calculates Matrix [A] and perform LU-descomp.\n";
    data << "           0 --> NO\n";
    data << "           1 --> YES\n";
    data << " \n";
    data <<"NOP6 = plot NORMALs to a Tecplot file\n";
    data <<"            0 --> NO\n";
    data <<"            1 --> YES\n";
    data << " \n";
    data <<"NOP7 = plot velocity field @ Trefftz's plane\n";
    data <<"            0 --> NO\n";
    data <<"            1 --> YES\n";
    return data;
}
